package cn.ztuo.bitrade.service;

import cn.ztuo.bitrade.constant.BooleanEnum;
import cn.ztuo.bitrade.dao.ContractPositionForceMapper;
import cn.ztuo.bitrade.entity.ContractCoin;
import cn.ztuo.bitrade.entity.ContractOrderPositionType;
import cn.ztuo.bitrade.entity.ContractPositionForce;
import cn.ztuo.bitrade.util.ContractUtils;
import cn.ztuo.bitrade.util.MessageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContractPositionForceService {

    @Autowired
    private ContractPositionForceMapper ContractPositionForceMapper;

    public List<ContractPositionForce> findPositions(ContractPositionForce position){
        Example<ContractPositionForce> example = Example.of(position);
        List<ContractPositionForce> list = ContractPositionForceMapper.findAll(example);
        list.stream().map(a->{
             ContractCoin coin = ContractUtils.getBaseSymbol(a.getSymbol());
             a.setBaseCoin(coin.getCoinSymbol());
             return a;
        }).collect(Collectors.toList());
        return list;
    }

    public List<ContractPositionForce> findByMemberIdAndSymbol(Long memberId, String symbol){
        return ContractPositionForceMapper.findByMemberIdAndSymbol(memberId,symbol);
    }
    public List<ContractPositionForce> findAll(){
        return ContractPositionForceMapper.findAllByStatusAndIsLock(0, BooleanEnum.IS_FALSE);
    }
    public ContractPositionForce getByPositionTypeAndSymbolAndMemberId(ContractOrderPositionType positionType, String symbol,Long memberId){
        return ContractPositionForceMapper.getByPositionTypeAndSymbolAndMemberId(positionType,symbol,memberId);
    }
    public ContractPositionForce getById(Long id){
        return ContractPositionForceMapper.getById(id);
    }
    public ContractPositionForce saveAndFlush(ContractPositionForce position){
        return ContractPositionForceMapper.saveAndFlush(position);
    }

    @Transactional(rollbackFor = Exception.class)
    public MessageResult updatePositionByDesignate(ContractPositionForce position,
                                     BigDecimal amount,
                                     Integer tradeNowp,
                                     BigDecimal avgPrice,
                                     BigDecimal bond) {
        int ret = ContractPositionForceMapper.updatePositionByDesignate(position.getId(),
                amount,
                tradeNowp,
                avgPrice,
                bond);
        if (ret > 0) {
            return MessageResult.success();
        } else {
            return MessageResult.error("Information Expired");
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public MessageResult updatePositionByThrowout(ContractPositionForce position,
                                     BigDecimal amount,
                                     Integer tradeNowp,
                                     BigDecimal bond) {
        int ret = ContractPositionForceMapper.updatePositionByThrowout(position.getId(),
                amount,
                tradeNowp,
                bond);
        if (ret > 0) {
            return MessageResult.success();
        } else {
            return MessageResult.error("Information Expired");
        }
    }

}
